package bit.pja.wolfkill.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Component
@Aspect
public class FlagAop {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Pointcut(value = "@annotation(bit.pja.wolfkill.aop.Flag)")
    public void flagAspect(){}


    @Before("flagAspect()")
    public void doBefore(JoinPoint joinPoint) {
        String methodName = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        logger.info("Before");
        logger.info(String.format("方法名: %s  参数: %s", methodName, Arrays.asList(args)));
    }


    @Around("flagAspect()")
    public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable{
        try {
            return joinPoint.proceed();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
